


public class MultiplicativeExpr extends AdditiveExpr {

	PrimaryExpr prExpr;
	MultiplicativeExpr multiExpr;
	int op;
	public static final int MD = 12;
	public static final int TO = 4;
	public static final int DO = 5;
	public MultiplicativeExpr() {

	}

	public MultiplicativeExpr(PrimaryExpr prExpr, MultiplicativeExpr multiExpr,
			int op) {
		super();
		this.prExpr = prExpr;
		this.multiExpr = multiExpr;
		this.op = op;
	}

	public void check() throws SemanticException {
		prExpr.check();
		if (multiExpr != null)
			multiExpr.check();
		checkType();
	}

	public int checkType() throws SemanticException {
		if (multiExpr != null) {
			// check both the same type
			if (multiExpr.checkType() != prExpr.checkType())
				throw new SemanticException(
						"Types of expression doesn't match: "
								+ multiExpr.checkType() + " , "
								+ prExpr.checkType());
			// check it is floar or int only
			int type = prExpr.checkType();
			if (type != Type.NM)
				throw new SemanticException(
						"Arethmetic expression can be done only on int or float");
		}
		return prExpr.checkType();
	}

	public String toString() {
		String ret = "MultiplicativeExpression\n";
		String s = "";
		if (multiExpr != null) {
			String ops = "";
			switch (op) {
			case Type.TO:
				ops = "*\n";
				break;
			case Type.DO:
				ops = "/\n";
				break;
			case Type.MD:
				ops = "%\n";
				break;
			}
			s += multiExpr.toString() + ops;
		}
		s += prExpr.toString();
		for (String st : s.split("\n"))
			ret += "| " + st + "\n";
		return ret;
	}
}
